package com.oocl.project5.parse;


import com.oocl.project5.doublelinklist.Node;
import com.oocl.project5.informationstore.Person;
import com.oocl.project5.printformat.PrintFormat;

public class CommandParseImp implements CommandParse {
	public int selectParse(String conmand){
		int n=7;
		if(conmand.equals("Q")){
			n=0;
		}else if(conmand.equals("A")||conmand.equals("a")){
			n=1;
		}else if(conmand.equals("D")||conmand.equals("d")){
			n=2;
		}else if(conmand.equals("U")||conmand.equals("u")){
			n=3;
		}else if(conmand.equals("L")||conmand.equals("l")){
			n=4;
		}else if(conmand.equals("S")||conmand.equals("s")){
			n=5;
		}else if(conmand.equals("H")||conmand.equals("h")){
			n=6;
		}
		
		return n;
	}
	public String dParse(String command){
		String[] arr1 = command.split("&");
		String cm ="";
		for(int i=0;i<arr1.length;i++){
			arr1[i] = "id:"+arr1[i];
			cm += arr1;
		}
		return cm;
	}
	public String lParse(Node<Person> headPointer,String command){
		if(command.equals("A")){
			PrintFormat.printPersonAll(headPointer);
			return "A";
		}else if(command.equals("C")){
			return "C";
		}else{
			PrintFormat.printError();
			return "E";
		}
	}
	public int uParse(Node<Person> headPointer,String command){
		String[] arr1 = command.split(",");
		Node<Person> curPointer;
		int len = arr1.length;
		int n = 0;
		try {
			for(int i=0;i<len;i++){
				String[] arr2 = arr1[i].split(":");
				if(arr2[0].equals("id")) {
					curPointer = headPointer;
					while(curPointer!=null){
						String curId =""+ curPointer.getData().getId();
						if(curId.equals(arr2[1])){
							return n;
						}
						n++;
						curPointer = curPointer.getNext();
						if(curPointer == null)
							return -1;
					}
				}else{
					PrintFormat.printErrorField(arr2[0]);
					return -1;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("qwqwq");
		}
		return -1;
	}

	public Person[] deleteParse(Node<Person> headPointer, String command) {
		Person[] persons = new Person[10];
		String[] arr1 = command.split(",");
		Node<Person> curPointer;
		int len = arr1.length;
		try {
			for(int i=0;i<len;i++){
				String[] arr2 = arr1[i].split(":");
				if(arr2[0].equals("id")) {
					curPointer = headPointer;
					while(curPointer!=null){
						String curId =""+ curPointer.getData().getId();
						if(curId.equals(arr2[1])){
							for(int j=0;j<10;j++){
								if(persons[j]==null){
									persons[j]=curPointer.getData();
								    break;
								}
							}
						}
						curPointer = curPointer.getNext();
					}
				}else{
					PrintFormat.printErrorField(arr2[0]);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("qwqwq");
		}
		return persons;
	}

	public Person[] addParse(Node<Person> headPointer, String command) {
		// TODO Auto-generated method stub
		Person newPerson = new Person();
		Person[] persons = {newPerson}; 
		Node<Person> curPointer = headPointer;
		while(curPointer.getNext()!=null)
			curPointer = curPointer.getNext();
		int id = curPointer.getData().getId();
		String[] arr1 = command.split(",");
		int len = arr1.length;
		try {
			for(int i=0;i<len;i++){
				String[] arr2 = arr1[i].split(":");
				if(arr2[0].equals("name")){
					newPerson.setName(arr2[1]);
				}else if (arr2[0].equals("sex")) {
					newPerson.setSex(arr2[1]);
				}else if (arr2[0].equals("birthday")) {
					newPerson.setBirthday(arr2[1]);
				}else if (arr2[0].equals("call")) {
					newPerson.setCall(arr2[1]);
				}else if(arr2[0].equals("address")) {
					newPerson.setAddress(arr2[1]);
				}else if(arr2[0].equals("id")) {
					System.out.println(" Id can only be generated automatically!!!");
				}else{
					PrintFormat.printErrorField(arr2[0]);
				}
			}
			if(newPerson.getName()!=null){
				newPerson.setId(id+1);
			}else {
				System.out.println(" Name can not be null !!!");
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("qwqwq");
		}
		return persons;
	}

	public Person[] updataParse(Node<Person> headPointer,int n,String command) {
		// TODO Auto-generated method stub
		Node<Person> curPointer = headPointer;
		int index=0;
		while(curPointer!=null&&index!=n){
		    index++;
			curPointer = curPointer.getNext();
		}
		Person person = new Person();
		person.setAddress(curPointer.getData().getAddress());
		person.setBirthday(curPointer.getData().getBirthday());
		person.setCall(curPointer.getData().getCall());
		person.setId(curPointer.getData().getId());
		person.setName(curPointer.getData().getName());
		person.setSex(curPointer.getData().getSex());
		Person[] persons = {person}; 
		String[] arr1 = command.split(",");
		int len = arr1.length;
		try {
			for(int i=0;i<len;i++){
				String[] arr2 = arr1[i].split(":");
				if(arr2[0].equals("name")){
					persons[0].setName(arr2[1]);
				}else if (arr2[0].equals("sex")) {
					persons[0].setSex(arr2[1]);
				}else if (arr2[0].equals("birthday")) {
					persons[0].setBirthday(arr2[1]);
				}else if (arr2[0].equals("call")) {
					persons[0].setCall(arr2[1]);
				}else if(arr2[0].equals("address")) {
					persons[0].setAddress(arr2[1]);
				}else if(arr2[0].equals("id")) {
					System.out.println(" Id can only be generated automatically!!!");
				}else{
					PrintFormat.printErrorField(arr2[0]);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("qwqwq");
		}
		return persons;
	}

	public Person[] lookParse(Node<Person> headPointer, String command) {
		// TODO Auto-generated method stub
		int len1 = headPointer.length(headPointer);
		Node<Person> curPointer1 = headPointer;
		Person[] personInit = new Person[len1];
		Person[] persons = new Person[len1];
		Person[] personTemp = new Person[len1];
		for(int i =0;i<len1;i++){
			personInit[i] = curPointer1.getData();
			personTemp[i] = curPointer1.getData();
			curPointer1 = curPointer1.getNext();
		}
		String[] arr1 = command.split(",");
		int len = arr1.length;
		int flag = 0;
		try {
			for(int i=0;i<len;i++){
			    flag =0;
				String[] arr2 = arr1[i].split(":");
				if(arr2[0].equals("name")){
					for(int k = 0;k<len1&&personTemp[k]!=null;k++){
						if(personTemp[k].getName().equals(arr2[1])){
							for(int j=0;j<10;j++){
								if(persons[j]==null){
									persons[j]=personTemp[k];
									flag =1;
								    break;
								}
							}
						}
					}
					for(int n =0;n<len1;n++){
						personTemp[n] = persons[n];
						persons[n]=null;
					}
				}else if(arr2[0].equals("sex")){
					for(int k = 0;k<len1&&personTemp[k]!=null;k++){
						if(personTemp[k].getSex().equals(arr2[1])){
							for(int j=0;j<len1;j++){
								if(persons[j]==null){
									persons[j]=personTemp[k];
									flag =1;
								    break;
								}
							}
						}
					}
					for(int n =0;n<len1;n++){
						personTemp[n] = persons[n];
						persons[n]=null;
					}
				}else if(arr2[0].equals("id")){
					for(int k = 0;k<len1&&personTemp[k]!=null;k++){
						String idString = ""+personTemp[k].getId();
						if(idString.equals(arr2[1])){
							for(int j=0;j<len1;j++){
								if(persons[j]==null){
									persons[j]=personTemp[k];
									flag =1;
								    break;
								}
							}
						}
					}
					for(int n =0;n<len1;n++){
						personTemp[n] = persons[n];
						persons[n]=null;
					}
				}else{
					PrintFormat.printError();
				}
			}
			if(flag==0){
				PrintFormat.printNotFind();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("qwqwq");
		}
		return personTemp;
	}

	public Person[] sortParse(Node<Person> headPointer, String command) {
		// TODO Auto-generated method stub
		Node<Person> curPointer = headPointer;
		int len = headPointer.length(headPointer);
		Person[] persons = new Person[len];
		for(int i =0;i<len;i++){
			persons[i] = curPointer.getData();
			curPointer = curPointer.getNext();
		}
		if(command.equals("id")){
			for(int i = 1;i<len;i++){
				for(int j=i;j>0;j--){
					if(persons[j].getId()<persons[j-1].getId()){
						Person temp =persons[j];
						persons[j]=persons[j-1];
						persons[j-1]=temp;
					}
				}
			}
		}else if(command.equals("name")){
			for(int i = 1;i<len;i++){
				for(int j=i;j>0;j--){
					if(persons[j].getName().compareTo(persons[j-1].getName())<0){
						Person temp =persons[j];
						persons[j]=persons[j-1];
						persons[j-1]=temp;
					}
				}
			}
		}else{
			PrintFormat.printError();
		}
		return persons;
	}

}
